<?php
namespace app\admin\controller;

use think\Loader;
use app\admin\service\Company AS CompanyService;
use app\admin\model\Company AS CompanyModel;

class Company extends Common {

    protected $companyService;
    protected $company;
    public function _initialize(){
        $this->companyService = new CompanyService();
        $this->company = new CompanyModel();
    }

    public function Index() {
        $where = [];
        $companyName = input('company_name');

        if($companyName){
            $where['c.company_name'] = ['like',"%{$companyName}%"];
        }
        $where['c.status'] = 0;
        $lists = $this->company->alias('c')
            ->join('stock s', 'c.sid=s.sid', 'left')
            ->field('c.*, c.sid AS c_sid, s.sid AS s_sid,s.stock_name, s.stock_code,s.jys')
            ->where($where)
            ->order('c.cid desc')
            // ->select();
            ->paginate(100);

        $this->assign('lists', $lists);
        $this->assign('name', $companyName);
        return $this->fetch();
    }

    //列表
    public function info() {

        $id = input('sid');
        if ($id) {
            //当前用户信息
            $info = $this->company->where(['sid'=>$id])->find();
            $this->assign('info', $info);
        }
        return $this->fetch();
    }
    //添加
    public function add() {
        $data = input();
        unset($data['sid']);
        if (!$data['stock_code']) {
            $this->error('编号不能为空');
        }

        if($data['jys']=='sh' || $data['jys']=='SH'){
            if($data['stock_code'][0]!='6'){
                $this->error('编号错误');
            }
        }
        if($data['jys']=='sz' || $data['jys']=='SZ'){
            if(($data['stock_code'][0] != '0') && ($data['stock_code'][0] != '3')){
                $this->error('编号错误');
            }
        }
        $this->companyService->Collect($data);
        //$this->success('操作成功', url('index'));
    }

    //删除
    public function del() {
        $sid = input('sid');
        $stock_code = input('stock_code');
        $this->company->delCompanyInfo($sid, $stock_code);
        $this->success('操作成功', url('index'));
    }
}